package com.pi.uc.service;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.pi.base.dto.result.respcode.user.UcResp;
import com.pi.base.enumerate.record.RecordState;
import com.pi.base.exception.ServiceException;
import com.pi.base.util.bean.ObjectTool;
import com.pi.comm.sequence.SnowflakeFactory;
import com.pi.uc.dao.entity.UcUserShareEntity;
import com.pi.uc.dao.entity.UcUserShareInfoEntity;
import com.pi.uc.dao.mapper.UcUserShareInfoMapper;
import com.pi.uc.dao.mapper.UcUserShareMapper;
import com.pi.uc.dao.param.UcUserShareParam;
import com.pi.uc.enumerate.UserShareChannel;

@Validated
@Service
public class UcUserShareService {
  @Autowired
  private UcUserService userService;
  @Autowired
  private UcUserShareInfoMapper userShareInfoMapper;
  @Autowired
  private UcUserShareMapper userShareMapper;
  
  /**
   * description 用户点击分享时生成的分享id
   * @param userId
   * @return
   */
  public Long generateShare(@NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId){
    userService.queryUserInfo(userId);
    UcUserShareInfoEntity userShareInfoEntity = new UcUserShareInfoEntity();
    userShareInfoEntity.setId(SnowflakeFactory.getDefaultWorker().nextId());
    userShareInfoEntity.setShareChannel(UserShareChannel.WEIXIN.getChannel());
    userShareInfoEntity.setShareUserId(userId);
    userShareInfoEntity.setState(RecordState.STATE_NORMAL.getCode());
    userShareInfoEntity.setId(SnowflakeFactory.getDefaultWorker().nextId());
    userShareInfoMapper.insert(userShareInfoEntity);
    return userShareInfoEntity.getId();
  }
  
  public void bindShare(@NotNull(message = "UC_SHARE.SHARE_ID_EMPTY") Long shareId, @NotNull(message = "UC_USER.USER_ID_EMPTY")  Long userId) {
    //查询分享事件是否存在
    UcUserShareInfoEntity shareInfo = getShareInfo(shareId);
    
    UcUserShareEntity entity = getShareRelationIfExists(shareId, userId);
    if(null == entity) {
      addShareBindInfo(shareId, userId, shareInfo.getShareUserId());
    }else {
      entity.setUserPv(entity.getUserPv() + 1);
      userShareMapper.updateById(entity);
    }
  }
  
  public UcUserShareInfoEntity getShareInfo(@NotNull(message = "UC_SHARE.SHARE_ID_EMPTY") Long shareId) { 
    UcUserShareInfoEntity shareInfo = getShareInfoIfExists(shareId);
    if(null == shareInfo) {
      throw new ServiceException("UC_SHARE.SHARE_INFO_EMPTY");
    }
    return shareInfo;
  }
  
  public UcUserShareInfoEntity getShareInfoIfExists(@NotNull(message = "UC_SHARE.SHARE_ID_EMPTY") Long shareId) { 
    UcUserShareInfoEntity shareInfo = userShareInfoMapper.findOne(shareId);
    return shareInfo;
  }
  
  public List<UcUserShareEntity> queryUserShare(@NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId){
    UcUserShareParam param = new UcUserShareParam();
    param.setShareUserId(userId);
    return userShareMapper.findList(param);
  }
  
  /**
   * @description 查询当前用户是否有分享过某人
   * @param shareUserId 当前登录用户(主动分享者)
   * @param userId 被分享用户编号
   * @return
   */
  public UcUserShareEntity queryUserShare(
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long shareUserId,
      @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId){
    UcUserShareParam param = new UcUserShareParam();
    param.setShareUserId(shareUserId);
    param.setUserId(userId);
    return ObjectTool.getOne(userShareMapper.findList(param));
  }
  
  public UcUserShareEntity getShareRelation(@NotNull(message = "UC_SHARE.SHARE_ID_EMPTY") Long shareId, @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId) { 
    UcUserShareEntity shareInfo = getShareRelationIfExists(shareId, userId);
    if(null == shareInfo) {
      throw new ServiceException(UcResp.RESP_SHARE_INFO_EMPTY);
    }
    return shareInfo;
  }
  
  public UcUserShareEntity getShareRelationIfExists(@NotNull(message = "UC_SHARE.SHARE_ID_EMPTY") Long shareId, @NotNull(message = "UC_USER.USER_ID_EMPTY") Long userId) { 
    UcUserShareParam param = new UcUserShareParam();
    param.setShareId(shareId);
    param.setUserId(userId);
    return ObjectTool.getOne(userShareMapper.findList(param));
  }
  
  private void addShareBindInfo( Long shareId, Long userId, Long shareUserId) {
    //添加绑定关系
    UcUserShareEntity entity = new UcUserShareEntity();
    entity.setShareId(shareId);
    entity.setShareState(RecordState.STATE_NORMAL.getCode());
    entity.setShareUserId(shareUserId);
    entity.setUserId(userId);
    entity.setUserPv(1);
    userShareMapper.insert(entity);
  }
}
